package types

import (
	"context"
	"time"
	proto "xagent/proto/v1"
)

// Task represents a unit of work to be performed
type Task struct {
	ID          string
	Name        string
	Description string
	Status      proto.TaskStatus
	Priority    int
	AgentID     string
	CreatedAt   time.Time
	UpdatedAt   time.Time
	StartedAt   *time.Time
	FinishedAt  *time.Time
	Result      interface{}
	Error       error
	Metadata    map[string]interface{}
}

// TaskScheduler defines the interface for task scheduling and management
type TaskScheduler interface {
	// Core scheduling methods
	ScheduleTask(ctx context.Context, task *proto.Task) error
	CancelTask(ctx context.Context, taskID string) error
	GetTaskStatus(ctx context.Context, taskID string) (proto.TaskStatus, error)

	// Queue management
	GetPendingTasks(ctx context.Context) ([]*proto.Task, error)
	GetTaskQueue() int
	GetMaxConcurrent() int

	// Resource management
	GetResourceUsage() map[string]float64
	GetAvailableResources() map[string]float64

	// Metrics
	GetMetrics() interface{}
}

// TaskProcessor defines the interface for task processing
type TaskProcessor interface {
	ProcessTask(task *proto.Task) error
	GetCapabilities() []string
	GetMetrics() interface{}
}

// TaskResult represents the result of a task execution
type TaskResult struct {
	TaskID    string
	Status    proto.TaskStatus
	Result    interface{}
	Error     error
	StartTime int64
	EndTime   int64
	Resources map[string]float64
	Metrics   map[string]interface{}
}

// TaskState represents the state of a task
type TaskState int

const (
	TaskStatePending TaskState = iota
	TaskStateRunning
	TaskStateCompleted
	TaskStateFailed
	TaskStateCancelled
	TaskStatePaused
)

func (s TaskState) String() string {
	switch s {
	case TaskStatePending:
		return "PENDING"
	case TaskStateRunning:
		return "RUNNING"
	case TaskStateCompleted:
		return "COMPLETED"
	case TaskStateFailed:
		return "FAILED"
	case TaskStateCancelled:
		return "CANCELLED"
	case TaskStatePaused:
		return "PAUSED"
	default:
		return "UNKNOWN"
	}
}
